package com.bwie.controller;

import com.alipay.api.AlipayApiException;
import com.bwie.pojo.Driver;
import com.bwie.pojo.Journey;
import com.bwie.pojo.Procedures;
import com.bwie.pojo.Volume;
import com.bwie.service.DriverService;
import com.bwie.service.JourneyService;
import com.bwie.service.ProcedsureService;
import com.bwie.service.VolumeService;
import com.bwie.unit.R;
import com.bwie.unit.ZhifubaoUtill;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.experimental.PackagePrivate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("journey")
public class JourneyController {

    @Autowired
    private JourneyService journeyService;

    @Autowired
    private ProcedsureService procedureService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private VolumeService volumeService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ZhifubaoUtill zhifubaoUtill;

    //查看行程列表
    @PostMapping("journeyList")
    public R journeyList(){
        List<Journey> list = journeyService.journeyList();
        return R.OK(list);
    }

    //预约车辆
    @PostMapping("journeyAdd")
    public R journeyAdd(@RequestBody Journey journey, @RequestHeader(name = "token") String token){
        Driver driver = new Driver();
        Claims body = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();
        Integer uid = body.get("uid", Integer.class);
        journey.setUserId(uid);
        journey.setJourneyTime(new Date());
        driver.setUserId(uid);
        driver.setDriverJourneyAddress(journey.getJourneyAddress());
        driver.setDriverJourneyPrice(journey.getJourneyPrice());
        driver.setDriverJourneyPoint(journey.getJourneyPoint());
        driver.setDriverJourneyTime(new Date());
        driverService.save(driver);
        journeyService.save(journey);
        return R.OK();
    }

    //回显订单
    @PostMapping("echoJourney/{id}")
    public R echoJourney(@PathVariable Integer id){
       Journey journey = journeyService.echoJourneyList(id);
       return R.OK(journey);
    }



    //取消订单
    @PostMapping("procedureAdd")
    public R procedureAdd(@RequestBody Journey journey,@RequestHeader(name = "token") String token){
        Procedures procedure = new Procedures();
        Claims body = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();
        Integer uid = body.get("uid", Integer.class);
        procedure.setUserId(uid);
        procedure.setProcedureMinutes(journey.getJourneyMinutes());
        procedure.setProcedurePrice(journey.getJourneyTake());
        procedure.setProcedureTime(new Date());
        journey.setJourneyStatus("1");
        journeyService.updateStatus(journey);
        procedureService.save(procedure);
        return R.OK();
    }

    //查看取消订单表
    @PostMapping("proceduresList")
    public R proceduresList(){
        List<Procedures> list = procedureService.proceduresList();
        return R.OK(list);
    }


    //查看司机列表
    @PostMapping("driverList")
    public R driverList(){
       List<Driver> list = driverService.driverList();
       return R.OK(list);
    }

    @PostMapping("volumeAdd")
    public R volumeAdd(@RequestBody Driver driver,@RequestHeader(name = "token") String token){
        Volume volume = new Volume();
        Claims body = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();
        Integer uid = body.get("uid", Integer.class);
        volume.setUserId(uid);
        volume.setVolumeJourneyPrice(driver.getDriverJourneyPrice());
        volume.setVolumePrice(10.00);
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE,5);
        volume.setVolumeTime(date);
        volume.setVolumeEndtime(cal.getTime());
        driver.setDriverStatus("1");
        volumeService.save(volume);
        driverService.updateStatus(driver);
        stringRedisTemplate.opsForValue().set("driver", String.valueOf(driver),20, TimeUnit.MINUTES);
        return R.OK();
    }

    @PostMapping("volumeList")
    public R volumeList(){
       List<Volume> list = volumeService.volumeList();
       return R.OK(list);
    }

    @RequestMapping("zfbPay/{id}/{jiege}")
    public String zfbPay(@PathVariable String id,@PathVariable String jiege) throws AlipayApiException {
        String pay = zhifubaoUtill.pay(id, jiege);
        return pay;
    }

    @RequestMapping("huidiao")
    public R huidiao(HttpServletRequest request) throws AlipayApiException {
        String id = request.getParameter("out_trade_no");
        Boolean aBoolean = zhifubaoUtill.queryResult(id);
        if (aBoolean){
            return R.OK("成功");
        }else {
            return R.ERROR(500,"失败");
        }
    }

}
